package com.example.demo.controller;

import com.example.demo.constant.Constants;
import com.example.demo.model.Result;
import com.example.demo.model.domian.User;
import com.example.demo.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @return user对象
     */
    @PostMapping("/login")
    @ResponseBody
    public Result<User> login(String username, String password, HttpServletRequest request){
        log.info("用户名为 username {}",username);
        //1.校验
        if (!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return Result.fail("账号或密码为空");
        }
        User temp = userService.login(username);
        //用户不存在
        if (temp==null){
            return Result.fail("用户不存在");
        }
        if (!password.equals(temp.getPassword())) {
            return Result.fail("密码错误");
        }
        request.getSession().setAttribute(Constants.USER_SESSION_KEY,temp);
        User safetyUser=new User();
        safetyUser.setUserId(temp.getUserId());
        safetyUser.setUsername(temp.getUsername());
        safetyUser.setScore(temp.getScore());
        safetyUser.setTotalCount(temp.getTotalCount());
        safetyUser.setWinCount(temp.getWinCount());
        return Result.success(safetyUser);

    }

    /**
     * 用户注册
     * @param username 用户名
     * @param password 密码
     * @return 1
     */
    @PostMapping("/register")
    @ResponseBody
    public Result<String> register(String username,String password){
        if (!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return Result.fail("账号或密码为空");
        }
        if (userService.login(username)!=null){
            return Result.fail("用户名存在");
        }
        Integer register = userService.register(username, password);
        return Result.success("注册成功"+register);

    }

    /**
     * 获取用户信息
     */
    @GetMapping("/userInfo")
    @ResponseBody
    public User getUSerInfo(HttpServletRequest request){
        User safetyUser=new User();
        try {
            HttpSession session = request.getSession();
            User attribute = (User) session.getAttribute(Constants.USER_SESSION_KEY);
            safetyUser.setScore(attribute.getScore());
            safetyUser.setUsername(attribute.getUsername());
            safetyUser.setTotalCount(attribute.getTotalCount());
            safetyUser.setWinCount(attribute.getWinCount());
            safetyUser.setUserId(attribute.getUserId());
        }catch (NullPointerException e){
            return null;
        }

        return safetyUser;
    }

}
